package com.qidian.qc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.pojo.qc.QcDefect;
import com.qidian.mes.util.pojo.MESResult;
import com.qidian.qc.mapper.QcDefectMapper;
import com.qidian.qc.service.QcDefectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 53邹李萍
 * @Company 起点编程
 * @Time 2023/10/26 15:09
 * @Description
 */
@Service
@Transactional
public class QcDefectServiceImpl implements QcDefectService {
    @Autowired
    QcDefectMapper qcDefectMapper;
    @Override
    public MESResult list(int page, int limit, QcDefect qcDefect) {
        //开启分页,在当前线程中执行下一条sql的时候是就会进行分页
        PageHelper.startPage(page,limit);
        List<QcDefect> list=qcDefectMapper.list(qcDefect);
        // 组织一个分页对象。（页码，每页大小，最大页码，数据，总条数)
        PageInfo<QcDefect> pageInfo= new PageInfo<>(list);
        return MESResult.ok(pageInfo);



    }

    @Override
    public MESResult save(QcDefect qcDefect) {

        //查重
        if (qcDefectMapper.getByDefectCode(qcDefect.getDefectCode())!=null ){
            return MESResult.duplicate("重复");
        }
        return MESResult.updateIsOk(qcDefectMapper.save(qcDefect));
    }

    @Override
    public MESResult update(QcDefect qcDefect) {
        //检验是否重复
        QcDefect qcDefect2=qcDefectMapper.getByDefectName(qcDefect.getDefectName());

        if(qcDefect2!=null&& !qcDefect2.getDefectCode().equals(qcDefect.getDefectCode())){
            return MESResult.duplicate("缺陷描述重复");
        }
        return MESResult.updateIsOk(qcDefectMapper.update(qcDefect));
    }

    @Override
    public MESResult getByDefectCode(String defectCode) {
        QcDefect qcDefect=null;
        if ((qcDefect=qcDefectMapper.getByDefectCode(defectCode))!=null)
        {
            return MESResult.ok(qcDefect);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult deleteByDefectCode(String defectCode) {

        return MESResult.updateIsOk(qcDefectMapper.deleteByDefectCode(defectCode));
    }

    @Override
    public MESResult deleteByDefectCodes(String[] deleteDefectCodes) {
        int a= qcDefectMapper.deleteByDefectCodes(deleteDefectCodes);
        return MESResult.ok(a+" ");
    }


}
